package code.sbxj.qwd.com.sbxj;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.CacheDiskUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.UnsupportedEncodingException;


@ContentView(R.layout.activity_checkline)
public class CheckLineActivity extends BaseActivity {

    @ViewInject(R.id.listview)
    private ListView listview;


    MyAdapter myadapter;
    JSONArray dataArray = new JSONArray();
    boolean isoffline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NfcUtils nfcUtils = new NfcUtils(this);
        isoffline = (boolean) CacheDiskUtils.getInstance().getSerializable("isoffline", false);
        setTitleText("巡检线：" + getIntent().getExtras().getString("linename"));
        initView();
//        getUserInfo();
        getTasklist(getIntent().getExtras().getInt("lineid"));

    }

    void initView() {
        myadapter = new MyAdapter();
        listview.setAdapter(myadapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Toast.makeText(CheckLineActivity.this,"请用设备扫描感应卡",Toast.LENGTH_SHORT).show();
//                try {
//                    CacheDiskUtils.getInstance().put("devicelist", dataArray.getJSONObject(i));
//                    Intent intent = new Intent();
//                    intent.setClass(CheckLineActivity.this, DeviceListActivity.class);
//                    intent.putExtra("devicename", dataArray.getJSONObject(i).optString("name"));
//                    intent.putExtra("linename", getIntent().getExtras().getString("linename"));
//                    intent.putExtra("deviceid", dataArray.getJSONObject(i).optInt("id"));
//                    intent.putExtra("lineid", getIntent().getExtras().getInt("lineid"));
//                    startActivity(intent);
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }

            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        //开启前台调度系统
        try {
            NfcUtils.mNfcAdapter.enableForegroundDispatch(this, NfcUtils.mPendingIntent, NfcUtils.mIntentFilter, NfcUtils.mTechList);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        //关闭前台调度系统

        try {
            NfcUtils.mNfcAdapter.disableForegroundDispatch(this);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        //当该Activity接收到NFC标签时，运行该方法
        //调用工具方法，读取NFC数据
        try {
            String str = NfcUtils.readNFCFromTag(intent).replace(" ","");
            if(str==null){
                return;
            }
            Toast.makeText(this, str, Toast.LENGTH_SHORT).show();

            if (dataArray == null) {
                return;
            }
            for (int i = 0; i < dataArray.length(); i++) {
                try {
                    if (dataArray.getJSONObject(i).optString("rfid").replace(" ","").contains(str)) {
//                        Intent intent1 = new Intent();
//                        intent1.setClass(DeviceListActivity.this, CheckItermActivity
//                                .class);
//                        intent1.putExtra("linename", getIntent().getExtras().getString("linename"));
//                        intent1.putExtra("lineid", getIntent().getExtras().getInt("lineid"));
//
//                        try {
//                            intent1.putExtra("deviceId", dataArray.getJSONObject(i).getInt("id"));
//                            intent1.putExtra("devicename", dataArray.getJSONObject(i).optString("name"));
//                            intent1.putExtra("data", dataArray.getJSONObject(i).toString());
//                            CacheDiskUtils.getInstance().put("checkiterm", dataArray.getJSONObject(i));
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//
//                        startActivity(intent1);
                        try {
                            CacheDiskUtils.getInstance().put("devicelist", dataArray.getJSONObject(i));
                            Intent intent1 = new Intent();
                            intent1.setClass(CheckLineActivity.this, DeviceListActivity.class);
                            intent1.putExtra("devicename", dataArray.getJSONObject(i).optString("name"));
                            intent1.putExtra("linename", getIntent().getExtras().getString("linename"));
                            intent1.putExtra("deviceid", dataArray.getJSONObject(i).optInt("id"));
                            intent1.putExtra("lineid", getIntent().getExtras().getInt("lineid"));
                            startActivity(intent1);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return dataArray.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return dataArray.get(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            convertView = getLayoutInflater().inflate(R.layout.activity_lineiterm, null);

            TextView linename = convertView.findViewById(R.id.linename);
            TextView temptv = convertView.findViewById(R.id.temptv);

            try {
                linename.setText("检查点：" + dataArray.getJSONObject(position).optString("name"));
                temptv.setText("查看设备");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return convertView;
        }


    }


    private void getTasklist(int lineid) {
        if (isoffline) {

            try {
                dataArray = CacheDiskUtils.getInstance().getJSONObject("offlinedata").getJSONArray("lineCheckPointList");
                myadapter.notifyDataSetChanged();
                return;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        ShowLoading();
        RequestParams params = new RequestParams(Config.BaseUrl + "line/devices?lineId=" + lineid);
        params.setUseCookie(true);
        params.addHeader("cookie", CacheDiskUtils.getInstance().getString("cookie"));
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Log.e("result", result);

                try {
                    dataArray = new JSONArray(result);
                    myadapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {
                CancleLoading();
            }
        });
    }


}
